import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '@/views/Main.vue'
import Index from '@/views/Index.vue'
import UserManage from '@/views/manage/UserManage.vue'
import Login from '@/views/login.vue'
import ClubManage from '@/views/manage/ClubManage.vue'
import ManagerInfo from '@/views/manage/ManagerInfo.vue'
import AccountSetting from '@/views/manage/AccountSetting.vue'

import UserHome from '@/views/user/UserHome.vue'
import UserInfo from '@/views/user/UserInfo.vue'
import UpdateAvatar from '@/views/user/UpdateAvatar.vue'
import UpdateUserInfo from '@/views/user/UpdateUserInfo.vue'
import PresidentHome from '@/views/president/PresidentHome.vue'
import Apply from '@/views/president/Apply.vue'
import InClubInfo from '@/views/club/InClubInfo.vue'
import InClubAct from '@/views/club/InClubAct.vue'
import ClubShow from '@/views/club/ClubShow.vue'
import clubIndex from '@/views/ClubIndex.vue'
import SelectAllMember from '@/views/club/SelectAllMember.vue'
import ApplicationResult from '@/views/user/ApplicationResult.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index',
    component: Main,

    children: [
      {
        path: '/index',
        name: 'index',
        component: Index,
      },
      {
        path: '/UserManage',
        name: 'UserManage',
        component: UserManage,
      },
      {
        path: '/ClubManage',
        name: 'ClubManage',
        component: ClubManage,
      },
      {
        path: '/ManagerInfo',
        name: 'ManagerInfo',
        component: ManagerInfo,
      },
      {
        path: '/AccountSetting',
        name: 'AccountSetting',
        component: AccountSetting,
      },
    ],
  },
  {
    path: '/ClubIndex',
    name: 'ClubIndex',
    component: clubIndex,
  },
  {
    path: '/ClubShow',
    name: 'ClubShow',
    component: ClubShow,
  },
  {
    path: '/UserHome',
    name: 'UserHome',
    component: UserHome,
  },
  {
    path: '/UserInfo',
    name: 'UserInfo',
    component: UserInfo,
  },
  {
    path: '/UpdateAvatar',
    name: 'UpdateAvatar',
    component: UpdateAvatar,
  },
  {
    path: '/UpdateUserInfo',
    name: 'UpdateUserInfo',
    component: UpdateUserInfo,
  },
  {
    path: '/ApplicationResult',
    name: 'ApplicationResult',
    component: ApplicationResult,
  },
  {
    path: '/PresidentHome',
    name: 'PresidentHome',
    component: PresidentHome,
  },
  {
    path: '/Apply',
    name: 'Apply',
    component: Apply,
  },

  {
    path: '/InClubInfo',
    name: 'InClubInfo',
    component: InClubInfo,
  },
  {
    path: '/InClubAct',
    name: 'InClubAct',
    component: InClubAct,
  },
  {
    path: '/SelectAllMember',
    name: 'SelectAllMember',
    component: SelectAllMember,
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const token = localStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})
export default router
